package com.ruoyi.biz.mapper;

import com.ruoyi.biz.domain.WarnedRecord;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * 【请填写功能名称】Mapper接口
 *
 * @author ruoyi
 * @date 2020-11-30
 */
public interface WarnedRecordMapper
{
    /**
     * 查询【请填写功能名称】
     *
     * @param warnedId 【请填写功能名称】ID
     * @return 【请填写功能名称】
     */
    public WarnedRecord selectWarnedRecordById(Long warnedId);

    /**
     * 查询【请填写功能名称】列表
     *
     * @param warnedRecord 【请填写功能名称】
     * @return 【请填写功能名称】集合
     */
    public List<WarnedRecord> selectWarnedRecordList(WarnedRecord warnedRecord);

    /**
     * 新增【请填写功能名称】
     *
     * @param warnedRecord 【请填写功能名称】
     * @return 结果
     */
    public int insertWarnedRecord(WarnedRecord warnedRecord);

    /**
     * 修改【请填写功能名称】
     *
     * @param warnedRecord 【请填写功能名称】
     * @return 结果
     */
    public int updateWarnedRecord(WarnedRecord warnedRecord);

    /**
     * 删除【请填写功能名称】
     *
     * @param warnedId 【请填写功能名称】ID
     * @return 结果
     */
    public int deleteWarnedRecordById(Long warnedId);

    /**
     * 批量删除【请填写功能名称】
     *
     * @param warnedIds 需要删除的数据ID
     * @return 结果
     */
    public int deleteWarnedRecordByIds(Long[] warnedIds);

    WarnedRecord selectByEquipmentIdForWarn(Long equipmentId);

    WarnedRecord selectByEquipmentIdForAlarm(Long equipmentId);

    @Select("")
    List<Map<String, Integer>> findTimeDataCount(Long equipmentId);

    @Select("select * from warned_record where warned_id=#{warnedId}")
    WarnedRecord selectWarnedRecord(Long warnedId);

    @Select("select * from warned_record where sensor_name=#{sensorName} order by create_time desc limit 1 ;")
    WarnedRecord selectBySensorName(String sensorName);

    @Select("select count(warned_id) as count from warned_record where sensor_name=#{sensorName};")
    int selectBySensorNameCount(String sensorName);

    @Select("select * from warned_record as w where type = #{type} and sensor_name = #{sensorName} order by create_time desc limit 1;")
    WarnedRecord findData(@Param("type") Integer type,@Param("sensorName") String sensorName);

    /**
     * MySQL5.5 独有的bug,一定要做限制，否则分组失效
     * 告警
     * @param equipmentId
     * @return
     */
    @Select("select count(*) as count,a.* ,s.notice_disable,s.sensor_id FROM (SELECT * FROM `warned_record` as w where w.equipment_id = #{equipmentId}  ORDER BY create_time desc limit 1000) as a left join sensor_info as s on s.sensor_name = a.sensor_name where a.equipment_id =#{equipmentId}  and a.type=2 GROUP BY a.sensor_name")
    List<WarnedRecord> findAlarmData(Long equipmentId);

    /**
     * MySQL5.5 独有的bug,一定要做限制，否则分组失效
     * 预警
     * @param equipmentId 27865846150
     * @return
     */
    @Select("select  count(*) as count,a.* ,s.notice_disable,s.sensor_id FROM (SELECT * FROM `warned_record` as w where w.equipment_id = #{equipmentId}  ORDER BY create_time desc limit 1000) as a left join sensor_info as s on s.sensor_name = a.sensor_name where a.equipment_id =#{equipmentId}  and a.type=1 GROUP BY a.sensor_name")
    List<WarnedRecord> findWarnData(Long equipmentId);

    @Select("SELECT count(*) FROM `warned_record` where sensor_name = #{sensorName} and type = #{type}")
    Integer selectSensorInfoNumsByName(@Param("sensorName") String sensorName,@Param("type") Integer type);
}
